package com.qfedu.edu.seckill.bo;

import com.qfedu.edu.seckill.po.User;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Data
public class SecurityUser implements UserDetails {

    private User user;

    //类型  0:管理员   1：会员
    private Integer type;

    //用户具有的权限和角色信息
    private List<String> roleAndPermStringList;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //申明这个权限和角色的集合
        List<GrantedAuthority> lists=new ArrayList<>();
        for (int i = 0; i <roleAndPermStringList.size() ; i++) {
            //拿到我们集合中权限和角色的信息
            String roleAndPermString= roleAndPermStringList.get(i);
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(roleAndPermString);
            lists.add(simpleGrantedAuthority);
        }
        return lists;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
